﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace StuMana.UI
{
    public partial class Cafeteria : Form
    {
        // declaration variants
        #region Variants

        #region datagridview parameters
       
        List<string>[] list;
        string[] columnName;
        string query;
        string[] columnNameReal;
        int page;
        int maxpage;
        static int rowperpage;

        private Services.DataviewService dataview = new Services.DataviewService();
        #endregion
        
        
        #region RFID reader
        Services.SL025M rfid_reader = new Services.SL025M();
        Services.COM connect;
        byte[] serial_buff = new byte[40];
        private Boolean isSelect = false;

        // get size of return byteArray
        private int size;
        private Int32 tagID = 0;
        private Int32 account = 0;
        private int curAcc=0;
        #endregion

        #endregion


        public Cafeteria()
        {
            InitializeComponent();
            Cafeteria_initials();
        }
        public Panel getPanel(int x, int y)
        {
            p_Cafeteria.Location = new Point(x, y);
            return p_Cafeteria;
        }
        private void Cafeteria_initials()
        { 
        }       

        // involving item activities
        #region Items

        // button click processing
        private void button6_Click(object sender, EventArgs e)
        {
            Button temp = (Button)sender;
            
            RFID_read();
            lb_oldmoney.Text = account.ToString();
            if (isSelect != false)
            {
                switch (temp.Tag.ToString())
                {
                    case "1":
                        if (account >= 10000)
                        {
                            account = account - 10000;
                            rfid_reader.bt_write(tagID, account);
                            connect.getdata(serial_buff);
                            if (serial_buff[3] == 0x00 && size != 5000)
                            {
                               MessageBox.Show("Ghi vao the thanh cong !");
                            }
                            else
                                MessageBox.Show("Ghi vao the that bai !");
                            listView1.Items.Add(new ListViewItem("Breakfast \n 10000".Split('\n')));
                        }
                        else
                            MessageBox.Show("Account not enough !", "Warning");
                        break;
                    case "2":
                        if (account >= 20000)
                        {
                            account = account - 20000;
                            rfid_reader.bt_write(tagID, account);
                            connect.getdata(serial_buff);
                            if (serial_buff[3] == 0x00 && size != 5000)
                            {
                               MessageBox.Show("Ghi vao the thanh cong !");
                            }
                            else
                                MessageBox.Show("Ghi vao the that bai !");
                            listView1.Items.Add(new ListViewItem("Lunch \n 20000".Split('\n')));
                        }
                        else
                            MessageBox.Show("Account not enough !", "Warning");
                        break;
                    case "3":
                        if (account >= 10000)
                        {
                            account = account - 10000;
                            rfid_reader.bt_write(tagID, account);
                            connect.getdata(serial_buff);
                            if (serial_buff[3] == 0x00 && size != 5000)
                            {
                               MessageBox.Show("Ghi vao the thanh cong !");
                            }
                            else
                                MessageBox.Show("Ghi vao the that bai !");
                            listView1.Items.Add(new ListViewItem("Revine \n 10000".Split('\n')));
                        }
                        else
                            MessageBox.Show("Account not enough !", "Warning");
                        break;
                    case "4":
                        if (account >= 10000)
                        {
                            account = account - 10000;
                            rfid_reader.bt_write(tagID, account);
                            connect.getdata(serial_buff);
                            if (serial_buff[3] == 0x00 && size != 5000)
                            {
                               MessageBox.Show("Ghi vao the thanh cong !");
                            }
                            else
                                MessageBox.Show("Ghi vao the that bai !");
                            listView1.Items.Add(new ListViewItem("Coffe \n 10000".Split('\n')));
                        }
                        else
                            MessageBox.Show("Account not enough !", "Warning");
                        break;
                    case "5":
                    case "6":
                        if (account >= 50000)
                        {
                            account = account - 50000;
                            rfid_reader.bt_write(tagID, account);
                            connect.getdata(serial_buff);
                            if (serial_buff[3] == 0x00 && size != 5000)
                            {
                              // MessageBox.Show("Ghi vao the thanh cong !");
                            }
                            else
                                MessageBox.Show("Ghi vao the that bai !");
                            listView1.Items.Add(new ListViewItem("Book \n 50000".Split('\n')));
                        }
                        else
                            MessageBox.Show("Account not enough !", "Warning");
                        break;
                        
                    default:
                        break;
                }
            }

            lb_tag.Text = tagID.ToString();
            lb_money.Text = account.ToString();
        }

        private void RFID_read()
        {
            try
            {
                rfid_reader.selectCard();
                size = connect.getdata(serial_buff);
                // login
                if (serial_buff[3] != 0x01 && size != 5000)
                {
                    ts_label.Text = "Checking";

                    // login to tag
                    rfid_reader.LogIn();
                    size = connect.getdata(serial_buff);

                    // ReadData from tag
                    if (serial_buff[3] == 0x02 && size != 5000)
                        rfid_reader.ReadData();
                    else
                    {                        
                        throw new Exception("Login fail");
                    }
                    size = connect.getdata(serial_buff);

                    //Receive data
                    if (serial_buff[3] == 0x00 && size != 5000)
                    {
                        tagID = get_student_tagID();
                        account = get_student_account();
                       // MessageBox.Show("Doc the thanh cong");
                        isSelect = true;
                    }
                    else
                    {
                        throw new Exception("Data isn't received");
                    }
                   
                }
                else if (serial_buff[3] == 0x01)
                {
                    isSelect = false;
                    resetvalues();
                    resetBillBoard();
                }
                else if (size == 5000)
                {
                    isSelect = false;
                    ts_label.Text = "No Signal";
                }

            }
            catch (Exception es)
            {
                isSelect = false;
                string msg = es.Message;
                MessageBox.Show(es.Message + "\nPlease try again.","Read Tag Fail");
            }
        }

        // change tagID into 
        private Int32 get_student_tagID()
        {

            Byte[] temp_byte = { 0x00, 0x00, 0x00, 0x00 };
            temp_byte[2] = serial_buff[6];
            temp_byte[1] = serial_buff[5];
            temp_byte[0] = serial_buff[4];

            // note: BitConverter.ToInt32(temp_byte, 0);  temp_byte must be at least 4byte
            Int32 temstr = BitConverter.ToInt32(temp_byte, 0);
            return temstr;
        }
        
        // change tagID into 
        private Int32 get_student_account()
        {
            Byte[] temp_byte = { 0x00, 0x00, 0x00, 0x00 };
            temp_byte[3] = serial_buff[10];
            temp_byte[2] = serial_buff[9];
            temp_byte[1] = serial_buff[8];
            temp_byte[0] = serial_buff[7];
            Int32 temstr = BitConverter.ToInt32(temp_byte, 0);
            return temstr;
        }

        // show infor into form
        private void show_TradeInfo()
        {
        }

        #endregion


        // involving billboard activities
        #region BillBoard

        //reset form
        private void resetBillBoard()
        {
            lb_name.Text = "NONE";
            lb_tag.Text = "NONE";
            lb_money.Text = "NONE";

            //dataGridView1.Rows.Clear();
            //dataGridView1.Refresh();
        }

        // reset values
        private void resetvalues()
        {
            account = 0;
            tagID = 0;
        }
        #endregion   

        #region connecting
        public void InitialConnect(StuMana.Services.COM conn)
        {
            connect = conn;
            rfid_reader.conn = conn.serialPort;
        }
        //set status to ts_connect
        public void setStatus(string stt)
        {
            ts_connect.Text = stt;
        }
        #endregion

        private void Cafeteria_Load(object sender, EventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void lb_tag_Click(object sender, EventArgs e)
        {

        }
    }
}
